import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

export type UserResult = {
  /** 状态码 200成功 */
  code: number;
  /** 接口返回信息 */
  message: string;
  /** 接口返回信息 */
  success: boolean;
  data: {
    /** 用户名 */
    id?: number;
    /** 用户名 */
    username: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
// export const getLogin = (data?: object) => {
//   return http.request<UserResult>("post", "/login", { data });
// };
export const getLogin = (data?: object) => {
  // const SysUserVO = {
  //   phoneNub: data.username,
  //   password: data.password
  // };
  return http.request<UserResult>("post", baseUrlApi("user/login/"), { data });
};

/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refreshToken", { data });
};

export type ResultType = {
  /** 状态码 200成功 */
  code: number;
  /** 接口返回信息 */
  message: string;
  /** 数据 */
  data: any;
};

/** 用户列表 pageNub：第几页 pageSize：每页大小*/
export const getUserList = (pageNub: number, pageSize: number, data?: object) => {
  return http.request<ResultType>("post", baseUrlApi("user/userList/" + pageNub + "/" + pageSize), { data });
};

/** 用户注册或添加 */
export const register = (data?: object) => {
  return http.request<ResultType>("post", baseUrlApi("user/register/"), { data });
};

/** 用户编辑 */
export const updateUserById = (data?: object) => {
  return http.request<ResultType>("post", baseUrlApi("user/updateUserById/"), { data });
};

/** 用户删除 */
export const deleteUserById = (id: number) => {
  return http.request<ResultType>("get", baseUrlApi("user/deleteUserById/" + id));
};

/** 用户信息查询通过id */
export const selectUserById = (id: number) => {
  return http.request<ResultType>("get", baseUrlApi("user/selectUserById/" + id));
};

/** 修改密码 id=用户id params={oldPassword=旧密码, newPassword=新密码}*/
export const updatePassword = (id: number, params?: object) => {
  return http.request<ResultType>("get", baseUrlApi("user/updatePassword/" + id), { params });
};
